From: Jeroen van der Heijden Date: Wed, 16 May 2018 14:22:09 +0000 (+0200) Subject: Work on tasks and idle time properties X-Git-Tag: archive/raspbian/2.0.44-1+rpi1~1^2~3^2~9^2~34 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/success//%22http:/www.example.com/cgi/success/?a=commitdiff_plain;h=e12734a985b9ab2ac6459458eaaf7a9d96966cfb;p=siridb-server.git Work on tasks and idle time properties --- diff --git a/grammar/gogrammar/grammar.go b/grammar/gogrammar/grammar.go index 89053c28..b9e12cff 100644 --- a/grammar/gogrammar/grammar.go +++ b/grammar/gogrammar/grammar.go @@ -4,7 +4,7 @@ package grammar // should be used with the goleri module. // // Source class: SiriGrammar -// Created at: 2018-03-30 21:57:58 +// Created at: 2018-05-16 16:15:46 import ( "regexp" @@ -118,6 +118,7 @@ const ( GidIntOperator = iota GidKAccess = iota GidKActiveHandles = iota + GidKActiveTasks = iota GidKAddress = iota GidKAfter = iota GidKAll = iota @@ -157,6 +158,8 @@ const ( GidKGroup = iota GidKGroups = iota GidKHelp = iota + GidKIdlePercentage = iota + GidKIdleTime = iota GidKIgnoreThreshold = iota GidKInfo = iota GidKInsert = iota @@ -315,6 +318,7 @@ func SiriGrammar() *goleri.Grammar { rComment := goleri.NewRegex(GidRComment, regexp.MustCompile(`^#.*`)) kAccess := goleri.NewKeyword(GidKAccess, "access", false) kActiveHandles := goleri.NewKeyword(GidKActiveHandles, "active_handles", false) + kActiveTasks := goleri.NewKeyword(GidKActiveTasks, "active_tasks", false) kAddress := goleri.NewKeyword(GidKAddress, "address", false) kAfter := goleri.NewKeyword(GidKAfter, "after", false) kAll := goleri.NewKeyword(GidKAll, "all", false) @@ -359,6 +363,8 @@ func SiriGrammar() *goleri.Grammar { goleri.NewKeyword(NoGid, "help", false), goleri.NewToken(NoGid, "?"), ) + kIdlePercentage := goleri.NewKeyword(GidKIdlePercentage, "idle_percentage", false) + kIdleTime := goleri.NewKeyword(GidKIdleTime, "idle_time", false) kInfo := goleri.NewKeyword(GidKInfo, "info", false) kIgnoreThreshold := goleri.NewKeyword(GidKIgnoreThreshold, "ignore_threshold", false) kInsert := goleri.NewKeyword(GidKInsert, "insert", false) @@ -573,7 +579,10 @@ func SiriGrammar() *goleri.Grammar { kStartupTime, kStatus, kActiveHandles, + kActiveTasks, kFifoFiles, + kIdlePercentage, + kIdleTime, kLogLevel, kMaxOpenFiles, kMemUsage, @@ -757,8 +766,11 @@ func SiriGrammar() *goleri.Grammar { NoGid, false, kActiveHandles, + kActiveTasks, kBufferSize, kFifoFiles, + kIdlePercentage, + kIdleTime, kPort, kPool, kStartupTime, @@ -1573,6 +1585,7 @@ func SiriGrammar() *goleri.Grammar { NoGid, false, kActiveHandles, + kActiveTasks, kBufferPath, kBufferSize, kDbname, @@ -1581,6 +1594,8 @@ func SiriGrammar() *goleri.Grammar { kDurationLog, kDurationNum, kFifoFiles, + kIdlePercentage, + kIdleTime, kIpSupport, kLibuv, kListLimit, @@ -1606,123 +1621,123 @@ func SiriGrammar() *goleri.Grammar { ), goleri.NewToken(NoGid, ","), 0, 0, false), ) timeitStmt := goleri.NewRepeat(GidTimeitStmt, kTimeit, 1, 1) - helpSelect := goleri.NewKeyword(GidHelpSelect, "select", false) - helpListGroups := goleri.NewKeyword(GidHelpListGroups, "groups", false) - helpListPools := goleri.NewKeyword(GidHelpListPools, "pools", false) - helpListShards := goleri.NewKeyword(GidHelpListShards, "shards", false) - helpListUsers := goleri.NewKeyword(GidHelpListUsers, "users", false) - helpListServers := goleri.NewKeyword(GidHelpListServers, "servers", false) - helpListSeries := goleri.NewKeyword(GidHelpListSeries, "series", false) - helpList := goleri.NewSequence( - GidHelpList, - kList, + helpAccess := goleri.NewKeyword(GidHelpAccess, "access", false) + helpAlterDatabase := goleri.NewKeyword(GidHelpAlterDatabase, "database", false) + helpAlterGroup := goleri.NewKeyword(GidHelpAlterGroup, "group", false) + helpAlterServer := goleri.NewKeyword(GidHelpAlterServer, "server", false) + helpAlterServers := goleri.NewKeyword(GidHelpAlterServers, "servers", false) + helpAlterUser := goleri.NewKeyword(GidHelpAlterUser, "user", false) + helpAlter := goleri.NewSequence( + GidHelpAlter, + kAlter, goleri.NewOptional(NoGid, goleri.NewChoice( NoGid, true, - helpListGroups, - helpListPools, - helpListShards, - helpListUsers, - helpListServers, - helpListSeries, + helpAlterDatabase, + helpAlterGroup, + helpAlterServer, + helpAlterServers, + helpAlterUser, )), ) - helpAccess := goleri.NewKeyword(GidHelpAccess, "access", false) - helpRevoke := goleri.NewKeyword(GidHelpRevoke, "revoke", false) - helpDropGroup := goleri.NewKeyword(GidHelpDropGroup, "group", false) - helpDropSeries := goleri.NewKeyword(GidHelpDropSeries, "series", false) - helpDropUser := goleri.NewKeyword(GidHelpDropUser, "user", false) - helpDropServer := goleri.NewKeyword(GidHelpDropServer, "server", false) - helpDropShards := goleri.NewKeyword(GidHelpDropShards, "shards", false) - helpDrop := goleri.NewSequence( - GidHelpDrop, - kDrop, + helpCountGroups := goleri.NewKeyword(GidHelpCountGroups, "groups", false) + helpCountPools := goleri.NewKeyword(GidHelpCountPools, "pools", false) + helpCountSeries := goleri.NewKeyword(GidHelpCountSeries, "series", false) + helpCountServers := goleri.NewKeyword(GidHelpCountServers, "servers", false) + helpCountShards := goleri.NewKeyword(GidHelpCountShards, "shards", false) + helpCountUsers := goleri.NewKeyword(GidHelpCountUsers, "users", false) + helpCount := goleri.NewSequence( + GidHelpCount, + kCount, goleri.NewOptional(NoGid, goleri.NewChoice( NoGid, true, - helpDropGroup, - helpDropSeries, - helpDropUser, - helpDropServer, - helpDropShards, + helpCountGroups, + helpCountPools, + helpCountSeries, + helpCountServers, + helpCountShards, + helpCountUsers, )), ) - helpCreateUser := goleri.NewKeyword(GidHelpCreateUser, "user", false) helpCreateGroup := goleri.NewKeyword(GidHelpCreateGroup, "group", false) + helpCreateUser := goleri.NewKeyword(GidHelpCreateUser, "user", false) helpCreate := goleri.NewSequence( GidHelpCreate, kCreate, goleri.NewOptional(NoGid, goleri.NewChoice( NoGid, true, - helpCreateUser, helpCreateGroup, + helpCreateUser, )), ) - helpCountGroups := goleri.NewKeyword(GidHelpCountGroups, "groups", false) - helpCountPools := goleri.NewKeyword(GidHelpCountPools, "pools", false) - helpCountShards := goleri.NewKeyword(GidHelpCountShards, "shards", false) - helpCountUsers := goleri.NewKeyword(GidHelpCountUsers, "users", false) - helpCountServers := goleri.NewKeyword(GidHelpCountServers, "servers", false) - helpCountSeries := goleri.NewKeyword(GidHelpCountSeries, "series", false) - helpCount := goleri.NewSequence( - GidHelpCount, - kCount, + helpDropGroup := goleri.NewKeyword(GidHelpDropGroup, "group", false) + helpDropSeries := goleri.NewKeyword(GidHelpDropSeries, "series", false) + helpDropServer := goleri.NewKeyword(GidHelpDropServer, "server", false) + helpDropShards := goleri.NewKeyword(GidHelpDropShards, "shards", false) + helpDropUser := goleri.NewKeyword(GidHelpDropUser, "user", false) + helpDrop := goleri.NewSequence( + GidHelpDrop, + kDrop, goleri.NewOptional(NoGid, goleri.NewChoice( NoGid, true, - helpCountGroups, - helpCountPools, - helpCountShards, - helpCountUsers, - helpCountServers, - helpCountSeries, + helpDropGroup, + helpDropSeries, + helpDropServer, + helpDropShards, + helpDropUser, )), ) - helpNoaccess := goleri.NewKeyword(GidHelpNoaccess, "noaccess", false) - helpAlterServer := goleri.NewKeyword(GidHelpAlterServer, "server", false) - helpAlterDatabase := goleri.NewKeyword(GidHelpAlterDatabase, "database", false) - helpAlterGroup := goleri.NewKeyword(GidHelpAlterGroup, "group", false) - helpAlterServers := goleri.NewKeyword(GidHelpAlterServers, "servers", false) - helpAlterUser := goleri.NewKeyword(GidHelpAlterUser, "user", false) - helpAlter := goleri.NewSequence( - GidHelpAlter, - kAlter, + helpFunctions := goleri.NewKeyword(GidHelpFunctions, "functions", false) + helpGrant := goleri.NewKeyword(GidHelpGrant, "grant", false) + helpListGroups := goleri.NewKeyword(GidHelpListGroups, "groups", false) + helpListPools := goleri.NewKeyword(GidHelpListPools, "pools", false) + helpListSeries := goleri.NewKeyword(GidHelpListSeries, "series", false) + helpListServers := goleri.NewKeyword(GidHelpListServers, "servers", false) + helpListShards := goleri.NewKeyword(GidHelpListShards, "shards", false) + helpListUsers := goleri.NewKeyword(GidHelpListUsers, "users", false) + helpList := goleri.NewSequence( + GidHelpList, + kList, goleri.NewOptional(NoGid, goleri.NewChoice( NoGid, true, - helpAlterServer, - helpAlterDatabase, - helpAlterGroup, - helpAlterServers, - helpAlterUser, + helpListGroups, + helpListPools, + helpListSeries, + helpListServers, + helpListShards, + helpListUsers, )), ) + helpNoaccess := goleri.NewKeyword(GidHelpNoaccess, "noaccess", false) + helpRevoke := goleri.NewKeyword(GidHelpRevoke, "revoke", false) + helpSelect := goleri.NewKeyword(GidHelpSelect, "select", false) helpShow := goleri.NewKeyword(GidHelpShow, "show", false) - helpTimezones := goleri.NewKeyword(GidHelpTimezones, "timezones", false) helpTimeit := goleri.NewKeyword(GidHelpTimeit, "timeit", false) - helpGrant := goleri.NewKeyword(GidHelpGrant, "grant", false) - helpFunctions := goleri.NewKeyword(GidHelpFunctions, "functions", false) + helpTimezones := goleri.NewKeyword(GidHelpTimezones, "timezones", false) help := goleri.NewSequence( GidHelp, kHelp, goleri.NewOptional(NoGid, goleri.NewChoice( NoGid, true, - helpSelect, - helpList, helpAccess, - helpRevoke, - helpDrop, - helpCreate, + helpAlter, helpCount, + helpCreate, + helpDrop, + helpFunctions, + helpGrant, + helpList, helpNoaccess, - helpAlter, + helpRevoke, + helpSelect, helpShow, - helpTimezones, helpTimeit, - helpGrant, - helpFunctions, + helpTimezones, )), ) START := goleri.NewSequence( diff --git a/grammar/grammar.py b/grammar/grammar.py index 8edc7c3e..3ac2da6f 100644 --- a/grammar/grammar.py +++ b/grammar/grammar.py @@ -46,6 +46,7 @@ class SiriGrammar(Grammar): # Keywords k_access = Keyword('access') k_active_handles = Keyword('active_handles') + k_active_tasks = Keyword('active_tasks') k_address = Keyword('address') k_after = Keyword('after') k_all = Keyword('all') @@ -85,6 +86,8 @@ class SiriGrammar(Grammar): k_group = Keyword('group') k_groups = Keyword('groups') k_help = Choice(Keyword('help'), Token('?')) + k_idle_percentage = Keyword('idle_percentage') + k_idle_time = Keyword('idle_time') k_info = Keyword('info') k_ignore_threshold = Keyword('ignore_threshold') k_insert = Keyword('insert') @@ -258,7 +261,10 @@ class SiriGrammar(Grammar): k_status, # Remote properties k_active_handles, + k_active_tasks, k_fifo_files, + k_idle_percentage, + k_idle_time, k_log_level, k_max_open_files, k_mem_usage, @@ -333,8 +339,11 @@ class SiriGrammar(Grammar): where_server = Sequence(k_where, Prio( Sequence(Choice( k_active_handles, + k_active_tasks, k_buffer_size, k_fifo_files, + k_idle_percentage, + k_idle_time, k_port, k_pool, k_startup_time, @@ -720,6 +729,7 @@ class SiriGrammar(Grammar): show_stmt = Sequence(k_show, List(Choice( k_active_handles, + k_active_tasks, k_buffer_path, k_buffer_size, k_dbname, @@ -728,6 +738,8 @@ class SiriGrammar(Grammar): k_duration_log, k_duration_num, k_fifo_files, + k_idle_percentage, + k_idle_time, k_ip_support, k_libuv, k_list_limit, diff --git a/include/siri/db/db.h b/include/siri/db/db.h index e75a10e1..7f7cc282 100644 --- a/include/siri/db/db.h +++ b/include/siri/db/db.h @@ -77,7 +77,6 @@ typedef struct siridb_s uint8_t flags; uint8_t pad0; uint32_t max_series_id; - uint16_t active_tasks; uint16_t insert_tasks; uint16_t shard_mask_num; uint16_t shard_mask_log; @@ -115,8 +114,11 @@ typedef struct siridb_s siridb_replicate_t * replicate; siridb_reindex_t * reindex; siridb_groups_t * groups; + siridb_tasks_t tasks; } siridb_t; +int32_t siridb_get_uptime(siridb_t * siridb); +int8_t siridb_get_idle_percentage(siridb_t * siridb); int siridb_is_db_path(const char * dbpath); siridb_t * siridb_new(const char * dbpath, int lock_flags); siridb_t * siridb_get(llist_t * siridb_list, const char * dbname); diff --git a/include/siri/db/tasks.h b/include/siri/db/tasks.h new file mode 100644 index 00000000..a0274a78 --- /dev/null +++ b/include/siri/db/tasks.h @@ -0,0 +1,32 @@ +/* + * tasks.h - SiriDB Error. + * + * author : Jeroen van der Heijden + * email : jeroen@transceptor.technology + * copyright : 2016, Transceptor Technology + * + * changes + * - initial version, 31-10-2016 + * + */ +#pragma once + +#include +#include +#include + +typedef struct siridb_tasks_s +{ + struct timespec _timeit; + uint64_t active; + double idle_time; +} siridb_tasks_t; + + +void siridb_tasks_init(siridb_tasks_t *tasks); + +#define siridb_tasks_inc(tasks) \ +if (!tasks.active++) tasks.idle_time += timeit_stop(&tasks._timeit) + +#define siridb_tasks_dec(tasks) \ +if (!--tasks.active) timeit_start(&tasks._timeit) diff --git a/include/siri/grammar/grammar.h b/include/siri/grammar/grammar.h index 2b747110..764dfc2c 100644 --- a/include/siri/grammar/grammar.h +++ b/include/siri/grammar/grammar.h @@ -5,7 +5,7 @@ * should be used with the libcleri module. * * Source class: SiriGrammar - * Created at: 2018-03-30 21:57:58 + * Created at: 2018-05-16 16:15:46 */ #ifndef CLERI_EXPORT_SIRI_GRAMMAR_GRAMMAR_H_ #define CLERI_EXPORT_SIRI_GRAMMAR_GRAMMAR_H_ @@ -118,6 +118,7 @@ enum cleri_grammar_ids { CLERI_GID_INT_OPERATOR, CLERI_GID_K_ACCESS, CLERI_GID_K_ACTIVE_HANDLES, + CLERI_GID_K_ACTIVE_TASKS, CLERI_GID_K_ADDRESS, CLERI_GID_K_AFTER, CLERI_GID_K_ALL, @@ -157,6 +158,8 @@ enum cleri_grammar_ids { CLERI_GID_K_GROUP, CLERI_GID_K_GROUPS, CLERI_GID_K_HELP, + CLERI_GID_K_IDLE_PERCENTAGE, + CLERI_GID_K_IDLE_TIME, CLERI_GID_K_IGNORE_THRESHOLD, CLERI_GID_K_INFO, CLERI_GID_K_INSERT, diff --git a/include/timeit/timeit.h b/include/timeit/timeit.h index b8a164e8..cc51056a 100644 --- a/include/timeit/timeit.h +++ b/include/timeit/timeit.h @@ -11,19 +11,16 @@ */ #pragma once -#include - -typedef struct timeval timeit_t; - -void timeit_start(timeit_t * start); -float timeit_stop(timeit_t * start); +#include +double timeit_stop(struct timespec * start); +#define timeit_start(start) clock_gettime(CLOCK_MONOTONIC, start) /* * Usage: * - * timeit_t start; + * struct timespec start; * timeit_start(&start); * * ... some code .... diff --git a/src/siri/db/db.c b/src/siri/db/db.c index 2c56cf0c..970e0b77 100644 --- a/src/siri/db/db.c +++ b/src/siri/db/db.c @@ -59,6 +59,25 @@ static int SIRIDB_from_unpacker( *siridb = NULL; \ return -1; +/* + * Returns the up-time in seconds. + */ +int32_t siridb_get_uptime(siridb_t * siridb) +{ + return (int32_t) (time(NULL) - siridb->start_ts); +} + +/* + * Returns a value in the range 0 and 100 representing how much percent sine + * up-time is idle time. + */ +int8_t siridb_get_idle_percentage(siridb_t * siridb) +{ + double uptime = (double) siridb_get_uptime(siridb); + return (int8_t) round(siridb->tasks.idle_time / uptime * 100.0f); +} + + /* * Check if at least database.conf and database.dat exist in the path. */ @@ -337,6 +356,9 @@ siridb_t * siridb_new(const char * dbpath, int lock_flags) /* start groups update thread */ siridb_groups_start(siridb->groups); + /* start tasks */ + siridb_tasks_init(&siridb->tasks); + log_info("Finished loading database: '%s'", siridb->dbname); return siridb; @@ -813,7 +835,6 @@ static siridb_t * SIRIDB_new(void) siridb->dbname = NULL; siridb->dbpath = NULL; siridb->ref = 1; - siridb->active_tasks = 0; siridb->insert_tasks = 0; siridb->flags = 0; siridb->buffer_path = NULL; diff --git a/src/siri/db/initsync.c b/src/siri/db/initsync.c index fb364dc9..b8c8ffdd 100644 --- a/src/siri/db/initsync.c +++ b/src/siri/db/initsync.c @@ -186,7 +186,7 @@ void siridb_initsync_run(uv_timer_t * timer) timer, (siridb->replicate->initsync->pkg == NULL) ? INITSYNC_work : INITSYNC_send, - INITSYNC_SLEEP * siridb->active_tasks, + INITSYNC_SLEEP * siridb->tasks.active, 0); } diff --git a/src/siri/db/insert.c b/src/siri/db/insert.c index d6d18710..8aead544 100644 --- a/src/siri/db/insert.c +++ b/src/siri/db/insert.c @@ -26,6 +26,7 @@ #include #include #include +#include #define MAX_INSERT_MSG 236 #define INSERT_TIMEOUT 300000 // 5 minutes @@ -349,7 +350,7 @@ int insert_init_backend_local( qp_next(&ilocal->unpacker, NULL); // map qp_next(&ilocal->unpacker, &ilocal->qp_series_name); // first series or end - siridb->active_tasks++; + siridb_tasks_inc(siridb->tasks); siridb->insert_tasks++; uv_async_init(siri.loop, handle, INSERT_local_task); @@ -477,7 +478,7 @@ static void INSERT_local_free_cb(uv_async_t * handle) ilocal->promise->cb(ilocal->promise, NULL, ilocal->status); - ilocal->siridb->active_tasks--; + siridb_tasks_dec(ilocal->siridb->tasks); ilocal->siridb->insert_tasks--; if (ilocal->pcache != NULL) { @@ -1057,8 +1058,9 @@ static int INSERT_init_local( qp_next(&ilocal->unpacker, NULL); // map qp_next(&ilocal->unpacker, &ilocal->qp_series_name); // first series or end - siridb->active_tasks++; + siridb_tasks_inc(siridb->tasks); siridb->insert_tasks++; + uv_async_init(siri.loop, handle, INSERT_local_task); uv_async_send(handle); diff --git a/src/siri/db/props.c b/src/siri/db/props.c index d3ae6885..0d564e35 100644 --- a/src/siri/db/props.c +++ b/src/siri/db/props.c @@ -38,6 +38,10 @@ static void prop_active_handles( siridb_t * siridb, qp_packer_t * packer, int map); +static void prop_active_tasks( + siridb_t * siridb, + qp_packer_t * packer, + int map); static void prop_buffer_path( siridb_t * siridb, qp_packer_t * packer, @@ -70,6 +74,14 @@ static void prop_fifo_files( siridb_t * siridb, qp_packer_t * packer, int map); +static void prop_idle_percentage( + siridb_t * siridb, + qp_packer_t * packer, + int map); +static void prop_idle_time( + siridb_t * siridb, + qp_packer_t * packer, + int map); static void prop_ip_support( siridb_t * siridb, qp_packer_t * packer, @@ -168,6 +180,8 @@ void siridb_init_props(void) siridb_props[CLERI_GID_K_ACTIVE_HANDLES - KW_OFFSET] = prop_active_handles; + siridb_props[CLERI_GID_K_ACTIVE_TASKS - KW_OFFSET] = + prop_active_tasks; siridb_props[CLERI_GID_K_BUFFER_PATH - KW_OFFSET] = prop_buffer_path; siridb_props[CLERI_GID_K_BUFFER_SIZE - KW_OFFSET] = @@ -184,6 +198,10 @@ void siridb_init_props(void) prop_duration_num; siridb_props[CLERI_GID_K_FIFO_FILES - KW_OFFSET] = prop_fifo_files; + siridb_props[CLERI_GID_K_IDLE_PERCENTAGE - KW_OFFSET] = + prop_idle_percentage; + siridb_props[CLERI_GID_K_IDLE_TIME - KW_OFFSET] = + prop_idle_time; siridb_props[CLERI_GID_K_IP_SUPPORT - KW_OFFSET] = prop_ip_support; siridb_props[CLERI_GID_K_LIBUV - KW_OFFSET] = @@ -239,6 +257,15 @@ static void prop_active_handles( qp_add_int32(packer, (int32_t) siri.loop->active_handles); } +static void prop_active_tasks( + siridb_t * siridb, + qp_packer_t * packer, + int map) +{ + SIRIDB_PROP_MAP("active_tasks", 12) + qp_add_int32(packer, (int32_t) siridb->tasks.active); +} + static void prop_buffer_path( siridb_t * siridb, qp_packer_t * packer, @@ -311,6 +338,24 @@ static void prop_fifo_files( qp_add_int32(packer, (int32_t) siridb_fifo_size(siridb->fifo)); } +static void prop_idle_percentage( + siridb_t * siridb, + qp_packer_t * packer, + int map) +{ + SIRIDB_PROP_MAP("idle_percentage", 15) + qp_add_int8(packer, siridb_get_idle_percentage(siridb)); +} + +static void prop_idle_time( + siridb_t * siridb, + qp_packer_t * packer, + int map) +{ + SIRIDB_PROP_MAP("idle_time", 9) + qp_add_int32(packer, (int32_t) siridb->tasks.idle_time); +} + static void prop_ip_support( siridb_t * siridb __attribute__((unused)), qp_packer_t * packer, @@ -487,7 +532,7 @@ static void prop_uptime( int map) { SIRIDB_PROP_MAP("uptime", 6) - qp_add_int32(packer, (int32_t) (time(NULL) - siridb->start_ts)); + qp_add_int32(packer, siridb_get_uptime(siridb)); } static void prop_uuid( diff --git a/src/siri/db/query.c b/src/siri/db/query.c index a50311ad..fe5f1428 100644 --- a/src/siri/db/query.c +++ b/src/siri/db/query.c @@ -70,6 +70,7 @@ void siridb_query_run( float factor, int flags) { + siridb_t * siridb; uv_async_t * handle = (uv_async_t *) malloc(sizeof(uv_async_t)); if (handle == NULL) { @@ -132,7 +133,8 @@ void siridb_query_run( } /* increment active tasks */ - ((sirinet_socket_t *) query->client->data)->siridb->active_tasks++; + siridb = ((sirinet_socket_t *) query->client->data)->siridb; + siridb_tasks_inc(siridb->tasks); /* send next call */ uv_async_init(siri.loop, handle, (uv_async_cb) QUERY_parse); @@ -143,9 +145,10 @@ void siridb_query_run( void siridb_query_free(uv_handle_t * handle) { siridb_query_t * query = (siridb_query_t *) handle->data; + siridb_t * siridb = ((sirinet_socket_t *) query->client->data)->siridb; /* decrement active tasks */ - ((sirinet_socket_t *) query->client->data)->siridb->active_tasks--; + siridb_tasks_dec(siridb->tasks); /* free query */ free(query->q); diff --git a/src/siri/db/reindex.c b/src/siri/db/reindex.c index dcd82ed0..36e7d7ed 100644 --- a/src/siri/db/reindex.c +++ b/src/siri/db/reindex.c @@ -423,7 +423,7 @@ static void REINDEX_next(siridb_t * siridb) uv_timer_start( siridb->reindex->timer, REINDEX_work, - REINDEX_SLEEP * siridb->active_tasks, + REINDEX_SLEEP * siridb->tasks.active, 0); break; diff --git a/src/siri/db/replicate.c b/src/siri/db/replicate.c index 5aecf6b6..3495bcd7 100644 --- a/src/siri/db/replicate.c +++ b/src/siri/db/replicate.c @@ -383,7 +383,7 @@ static void REPLICATE_on_repl_response( uv_timer_start( siridb->replicate->timer, REPLICATE_work, - REPLICATE_SLEEP * siridb->active_tasks, + REPLICATE_SLEEP * siridb->tasks.active, 0); } sirinet_promise_decref(promise); diff --git a/src/siri/db/server.c b/src/siri/db/server.c index 3de59181..1150f2bd 100644 --- a/src/siri/db/server.c +++ b/src/siri/db/server.c @@ -1157,7 +1157,7 @@ int siridb_server_cexpr_cb( case CLERI_GID_K_UPTIME: return cexpr_int_cmp( cond->operator, - (int64_t) (time(NULL) - wserver->siridb->start_ts), + (int64_t) siridb_get_uptime(wserver->siridb), cond->int64); case CLERI_GID_K_ACTIVE_HANDLES: @@ -1166,6 +1166,12 @@ int siridb_server_cexpr_cb( (int64_t) siri.loop->active_handles, cond->int64); + case CLERI_GID_K_ACTIVE_TASKS: + return cexpr_int_cmp( + cond->operator, + (int64_t) wserver->siridb->tasks.active, + cond->int64); + case CLERI_GID_K_REINDEX_PROGRESS: return cexpr_str_cmp( cond->operator, diff --git a/src/siri/db/shard.c b/src/siri/db/shard.c index 801e3e48..5eafade8 100644 --- a/src/siri/db/shard.c +++ b/src/siri/db/shard.c @@ -1302,7 +1302,7 @@ int siridb_shard_optimize(siridb_shard_t * shard, siridb_t * siridb) /* make this sleep depending on the active_tasks * (50ms per active task) */ - usleep( 50000 * siridb->active_tasks + 100 ); + usleep( 50000 * siridb->tasks.active + 100 ); } siridb_series_decref(series); diff --git a/src/siri/db/tasks.c b/src/siri/db/tasks.c new file mode 100644 index 00000000..2d0bc494 --- /dev/null +++ b/src/siri/db/tasks.c @@ -0,0 +1,20 @@ +/* + * tasks.c - SiriDB Error. + * + * author : Jeroen van der Heijden + * email : jeroen@transceptor.technology + * copyright : 2016, Transceptor Technology + * + * changes + * - initial version, 31-10-2016 + * + */ +#include +#include + +void siridb_tasks_init(siridb_tasks_t * tasks) +{ + tasks->active = 0; + tasks->idle_time = 0.0f; + timeit_start(&tasks->_timeit); +} diff --git a/src/siri/grammar/grammar.c b/src/siri/grammar/grammar.c index f7e88b22..da6c574b 100644 --- a/src/siri/grammar/grammar.c +++ b/src/siri/grammar/grammar.c @@ -5,7 +5,7 @@ * should be used with the libcleri module. * * Source class: SiriGrammar - * Created at: 2018-03-30 21:57:58 + * Created at: 2018-05-16 16:15:46 */ #include "siri/grammar/grammar.h" @@ -31,6 +31,7 @@ cleri_grammar_t * compile_grammar(void) cleri_t * r_comment = cleri_regex(CLERI_GID_R_COMMENT, "^#.*"); cleri_t * k_access = cleri_keyword(CLERI_GID_K_ACCESS, "access", CLERI_CASE_SENSITIVE); cleri_t * k_active_handles = cleri_keyword(CLERI_GID_K_ACTIVE_HANDLES, "active_handles", CLERI_CASE_SENSITIVE); + cleri_t * k_active_tasks = cleri_keyword(CLERI_GID_K_ACTIVE_TASKS, "active_tasks", CLERI_CASE_SENSITIVE); cleri_t * k_address = cleri_keyword(CLERI_GID_K_ADDRESS, "address", CLERI_CASE_SENSITIVE); cleri_t * k_after = cleri_keyword(CLERI_GID_K_AFTER, "after", CLERI_CASE_SENSITIVE); cleri_t * k_all = cleri_keyword(CLERI_GID_K_ALL, "all", CLERI_CASE_SENSITIVE); @@ -76,6 +77,8 @@ cleri_grammar_t * compile_grammar(void) cleri_keyword(CLERI_NONE, "help", CLERI_CASE_SENSITIVE), cleri_token(CLERI_NONE, "?") ); + cleri_t * k_idle_percentage = cleri_keyword(CLERI_GID_K_IDLE_PERCENTAGE, "idle_percentage", CLERI_CASE_SENSITIVE); + cleri_t * k_idle_time = cleri_keyword(CLERI_GID_K_IDLE_TIME, "idle_time", CLERI_CASE_SENSITIVE); cleri_t * k_info = cleri_keyword(CLERI_GID_K_INFO, "info", CLERI_CASE_SENSITIVE); cleri_t * k_ignore_threshold = cleri_keyword(CLERI_GID_K_IGNORE_THRESHOLD, "ignore_threshold", CLERI_CASE_SENSITIVE); cleri_t * k_insert = cleri_keyword(CLERI_GID_K_INSERT, "insert", CLERI_CASE_SENSITIVE); @@ -291,7 +294,7 @@ cleri_grammar_t * compile_grammar(void) cleri_t * server_columns = cleri_list(CLERI_GID_SERVER_COLUMNS, cleri_choice( CLERI_NONE, CLERI_FIRST_MATCH, - 25, + 28, k_address, k_buffer_path, k_buffer_size, @@ -307,7 +310,10 @@ cleri_grammar_t * compile_grammar(void) k_startup_time, k_status, k_active_handles, + k_active_tasks, k_fifo_files, + k_idle_percentage, + k_idle_time, k_log_level, k_max_open_files, k_mem_usage, @@ -522,10 +528,13 @@ cleri_grammar_t * compile_grammar(void) cleri_choice( CLERI_NONE, CLERI_FIRST_MATCH, - 12, + 15, k_active_handles, + k_active_tasks, k_buffer_size, k_fifo_files, + k_idle_percentage, + k_idle_time, k_port, k_pool, k_startup_time, @@ -1463,8 +1472,9 @@ cleri_grammar_t * compile_grammar(void) cleri_list(CLERI_NONE, cleri_choice( CLERI_NONE, CLERI_FIRST_MATCH, - 31, + 34, k_active_handles, + k_active_tasks, k_buffer_path, k_buffer_size, k_dbname, @@ -1473,6 +1483,8 @@ cleri_grammar_t * compile_grammar(void) k_duration_log, k_duration_num, k_fifo_files, + k_idle_percentage, + k_idle_time, k_ip_support, k_libuv, k_list_limit, @@ -1498,53 +1510,51 @@ cleri_grammar_t * compile_grammar(void) ), cleri_token(CLERI_NONE, ","), 0, 0, 0) ); cleri_t * timeit_stmt = cleri_dup(CLERI_GID_TIMEIT_STMT, k_timeit); - cleri_t * help_select = cleri_keyword(CLERI_GID_HELP_SELECT, "select", CLERI_CASE_SENSITIVE); - cleri_t * help_list_groups = cleri_keyword(CLERI_GID_HELP_LIST_GROUPS, "groups", CLERI_CASE_SENSITIVE); - cleri_t * help_list_pools = cleri_keyword(CLERI_GID_HELP_LIST_POOLS, "pools", CLERI_CASE_SENSITIVE); - cleri_t * help_list_shards = cleri_keyword(CLERI_GID_HELP_LIST_SHARDS, "shards", CLERI_CASE_SENSITIVE); - cleri_t * help_list_users = cleri_keyword(CLERI_GID_HELP_LIST_USERS, "users", CLERI_CASE_SENSITIVE); - cleri_t * help_list_servers = cleri_keyword(CLERI_GID_HELP_LIST_SERVERS, "servers", CLERI_CASE_SENSITIVE); - cleri_t * help_list_series = cleri_keyword(CLERI_GID_HELP_LIST_SERIES, "series", CLERI_CASE_SENSITIVE); - cleri_t * help_list = cleri_sequence( - CLERI_GID_HELP_LIST, + cleri_t * help_access = cleri_keyword(CLERI_GID_HELP_ACCESS, "access", CLERI_CASE_SENSITIVE); + cleri_t * help_alter_database = cleri_keyword(CLERI_GID_HELP_ALTER_DATABASE, "database", CLERI_CASE_SENSITIVE); + cleri_t * help_alter_group = cleri_keyword(CLERI_GID_HELP_ALTER_GROUP, "group", CLERI_CASE_SENSITIVE); + cleri_t * help_alter_server = cleri_keyword(CLERI_GID_HELP_ALTER_SERVER, "server", CLERI_CASE_SENSITIVE); + cleri_t * help_alter_servers = cleri_keyword(CLERI_GID_HELP_ALTER_SERVERS, "servers", CLERI_CASE_SENSITIVE); + cleri_t * help_alter_user = cleri_keyword(CLERI_GID_HELP_ALTER_USER, "user", CLERI_CASE_SENSITIVE); + cleri_t * help_alter = cleri_sequence( + CLERI_GID_HELP_ALTER, 2, - k_list, + k_alter, cleri_optional(CLERI_NONE, cleri_choice( CLERI_NONE, CLERI_MOST_GREEDY, - 6, - help_list_groups, - help_list_pools, - help_list_shards, - help_list_users, - help_list_servers, - help_list_series + 5, + help_alter_database, + help_alter_group, + help_alter_server, + help_alter_servers, + help_alter_user )) ); - cleri_t * help_access = cleri_keyword(CLERI_GID_HELP_ACCESS, "access", CLERI_CASE_SENSITIVE); - cleri_t * help_revoke = cleri_keyword(CLERI_GID_HELP_REVOKE, "revoke", CLERI_CASE_SENSITIVE); - cleri_t * help_drop_group = cleri_keyword(CLERI_GID_HELP_DROP_GROUP, "group", CLERI_CASE_SENSITIVE); - cleri_t * help_drop_series = cleri_keyword(CLERI_GID_HELP_DROP_SERIES, "series", CLERI_CASE_SENSITIVE); - cleri_t * help_drop_user = cleri_keyword(CLERI_GID_HELP_DROP_USER, "user", CLERI_CASE_SENSITIVE); - cleri_t * help_drop_server = cleri_keyword(CLERI_GID_HELP_DROP_SERVER, "server", CLERI_CASE_SENSITIVE); - cleri_t * help_drop_shards = cleri_keyword(CLERI_GID_HELP_DROP_SHARDS, "shards", CLERI_CASE_SENSITIVE); - cleri_t * help_drop = cleri_sequence( - CLERI_GID_HELP_DROP, + cleri_t * help_count_groups = cleri_keyword(CLERI_GID_HELP_COUNT_GROUPS, "groups", CLERI_CASE_SENSITIVE); + cleri_t * help_count_pools = cleri_keyword(CLERI_GID_HELP_COUNT_POOLS, "pools", CLERI_CASE_SENSITIVE); + cleri_t * help_count_series = cleri_keyword(CLERI_GID_HELP_COUNT_SERIES, "series", CLERI_CASE_SENSITIVE); + cleri_t * help_count_servers = cleri_keyword(CLERI_GID_HELP_COUNT_SERVERS, "servers", CLERI_CASE_SENSITIVE); + cleri_t * help_count_shards = cleri_keyword(CLERI_GID_HELP_COUNT_SHARDS, "shards", CLERI_CASE_SENSITIVE); + cleri_t * help_count_users = cleri_keyword(CLERI_GID_HELP_COUNT_USERS, "users", CLERI_CASE_SENSITIVE); + cleri_t * help_count = cleri_sequence( + CLERI_GID_HELP_COUNT, 2, - k_drop, + k_count, cleri_optional(CLERI_NONE, cleri_choice( CLERI_NONE, CLERI_MOST_GREEDY, - 5, - help_drop_group, - help_drop_series, - help_drop_user, - help_drop_server, - help_drop_shards + 6, + help_count_groups, + help_count_pools, + help_count_series, + help_count_servers, + help_count_shards, + help_count_users )) ); - cleri_t * help_create_user = cleri_keyword(CLERI_GID_HELP_CREATE_USER, "user", CLERI_CASE_SENSITIVE); cleri_t * help_create_group = cleri_keyword(CLERI_GID_HELP_CREATE_GROUP, "group", CLERI_CASE_SENSITIVE); + cleri_t * help_create_user = cleri_keyword(CLERI_GID_HELP_CREATE_USER, "user", CLERI_CASE_SENSITIVE); cleri_t * help_create = cleri_sequence( CLERI_GID_HELP_CREATE, 2, @@ -1553,58 +1563,60 @@ cleri_grammar_t * compile_grammar(void) CLERI_NONE, CLERI_MOST_GREEDY, 2, - help_create_user, - help_create_group + help_create_group, + help_create_user )) ); - cleri_t * help_count_groups = cleri_keyword(CLERI_GID_HELP_COUNT_GROUPS, "groups", CLERI_CASE_SENSITIVE); - cleri_t * help_count_pools = cleri_keyword(CLERI_GID_HELP_COUNT_POOLS, "pools", CLERI_CASE_SENSITIVE); - cleri_t * help_count_shards = cleri_keyword(CLERI_GID_HELP_COUNT_SHARDS, "shards", CLERI_CASE_SENSITIVE); - cleri_t * help_count_users = cleri_keyword(CLERI_GID_HELP_COUNT_USERS, "users", CLERI_CASE_SENSITIVE); - cleri_t * help_count_servers = cleri_keyword(CLERI_GID_HELP_COUNT_SERVERS, "servers", CLERI_CASE_SENSITIVE); - cleri_t * help_count_series = cleri_keyword(CLERI_GID_HELP_COUNT_SERIES, "series", CLERI_CASE_SENSITIVE); - cleri_t * help_count = cleri_sequence( - CLERI_GID_HELP_COUNT, + cleri_t * help_drop_group = cleri_keyword(CLERI_GID_HELP_DROP_GROUP, "group", CLERI_CASE_SENSITIVE); + cleri_t * help_drop_series = cleri_keyword(CLERI_GID_HELP_DROP_SERIES, "series", CLERI_CASE_SENSITIVE); + cleri_t * help_drop_server = cleri_keyword(CLERI_GID_HELP_DROP_SERVER, "server", CLERI_CASE_SENSITIVE); + cleri_t * help_drop_shards = cleri_keyword(CLERI_GID_HELP_DROP_SHARDS, "shards", CLERI_CASE_SENSITIVE); + cleri_t * help_drop_user = cleri_keyword(CLERI_GID_HELP_DROP_USER, "user", CLERI_CASE_SENSITIVE); + cleri_t * help_drop = cleri_sequence( + CLERI_GID_HELP_DROP, 2, - k_count, + k_drop, cleri_optional(CLERI_NONE, cleri_choice( CLERI_NONE, CLERI_MOST_GREEDY, - 6, - help_count_groups, - help_count_pools, - help_count_shards, - help_count_users, - help_count_servers, - help_count_series + 5, + help_drop_group, + help_drop_series, + help_drop_server, + help_drop_shards, + help_drop_user )) ); - cleri_t * help_noaccess = cleri_keyword(CLERI_GID_HELP_NOACCESS, "noaccess", CLERI_CASE_SENSITIVE); - cleri_t * help_alter_server = cleri_keyword(CLERI_GID_HELP_ALTER_SERVER, "server", CLERI_CASE_SENSITIVE); - cleri_t * help_alter_database = cleri_keyword(CLERI_GID_HELP_ALTER_DATABASE, "database", CLERI_CASE_SENSITIVE); - cleri_t * help_alter_group = cleri_keyword(CLERI_GID_HELP_ALTER_GROUP, "group", CLERI_CASE_SENSITIVE); - cleri_t * help_alter_servers = cleri_keyword(CLERI_GID_HELP_ALTER_SERVERS, "servers", CLERI_CASE_SENSITIVE); - cleri_t * help_alter_user = cleri_keyword(CLERI_GID_HELP_ALTER_USER, "user", CLERI_CASE_SENSITIVE); - cleri_t * help_alter = cleri_sequence( - CLERI_GID_HELP_ALTER, + cleri_t * help_functions = cleri_keyword(CLERI_GID_HELP_FUNCTIONS, "functions", CLERI_CASE_SENSITIVE); + cleri_t * help_grant = cleri_keyword(CLERI_GID_HELP_GRANT, "grant", CLERI_CASE_SENSITIVE); + cleri_t * help_list_groups = cleri_keyword(CLERI_GID_HELP_LIST_GROUPS, "groups", CLERI_CASE_SENSITIVE); + cleri_t * help_list_pools = cleri_keyword(CLERI_GID_HELP_LIST_POOLS, "pools", CLERI_CASE_SENSITIVE); + cleri_t * help_list_series = cleri_keyword(CLERI_GID_HELP_LIST_SERIES, "series", CLERI_CASE_SENSITIVE); + cleri_t * help_list_servers = cleri_keyword(CLERI_GID_HELP_LIST_SERVERS, "servers", CLERI_CASE_SENSITIVE); + cleri_t * help_list_shards = cleri_keyword(CLERI_GID_HELP_LIST_SHARDS, "shards", CLERI_CASE_SENSITIVE); + cleri_t * help_list_users = cleri_keyword(CLERI_GID_HELP_LIST_USERS, "users", CLERI_CASE_SENSITIVE); + cleri_t * help_list = cleri_sequence( + CLERI_GID_HELP_LIST, 2, - k_alter, + k_list, cleri_optional(CLERI_NONE, cleri_choice( CLERI_NONE, CLERI_MOST_GREEDY, - 5, - help_alter_server, - help_alter_database, - help_alter_group, - help_alter_servers, - help_alter_user + 6, + help_list_groups, + help_list_pools, + help_list_series, + help_list_servers, + help_list_shards, + help_list_users )) ); + cleri_t * help_noaccess = cleri_keyword(CLERI_GID_HELP_NOACCESS, "noaccess", CLERI_CASE_SENSITIVE); + cleri_t * help_revoke = cleri_keyword(CLERI_GID_HELP_REVOKE, "revoke", CLERI_CASE_SENSITIVE); + cleri_t * help_select = cleri_keyword(CLERI_GID_HELP_SELECT, "select", CLERI_CASE_SENSITIVE); cleri_t * help_show = cleri_keyword(CLERI_GID_HELP_SHOW, "show", CLERI_CASE_SENSITIVE); - cleri_t * help_timezones = cleri_keyword(CLERI_GID_HELP_TIMEZONES, "timezones", CLERI_CASE_SENSITIVE); cleri_t * help_timeit = cleri_keyword(CLERI_GID_HELP_TIMEIT, "timeit", CLERI_CASE_SENSITIVE); - cleri_t * help_grant = cleri_keyword(CLERI_GID_HELP_GRANT, "grant", CLERI_CASE_SENSITIVE); - cleri_t * help_functions = cleri_keyword(CLERI_GID_HELP_FUNCTIONS, "functions", CLERI_CASE_SENSITIVE); + cleri_t * help_timezones = cleri_keyword(CLERI_GID_HELP_TIMEZONES, "timezones", CLERI_CASE_SENSITIVE); cleri_t * help = cleri_sequence( CLERI_GID_HELP, 2, @@ -1613,20 +1625,20 @@ cleri_grammar_t * compile_grammar(void) CLERI_NONE, CLERI_MOST_GREEDY, 14, - help_select, - help_list, help_access, - help_revoke, - help_drop, - help_create, + help_alter, help_count, + help_create, + help_drop, + help_functions, + help_grant, + help_list, help_noaccess, - help_alter, + help_revoke, + help_select, help_show, - help_timezones, help_timeit, - help_grant, - help_functions + help_timezones )) ); cleri_t * START = cleri_sequence( diff --git a/src/test/test.c b/src/test/test.c index 317f45de..4d6a8376 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -1017,7 +1017,7 @@ int test_strx_to_double(void) int run_tests(void) { - timeit_t start; + struct timespec start; timeit_start(&start); int rc = 0; rc += test_qpack(); @@ -1050,7 +1050,7 @@ int run_tests(void) rc += test_strx_to_double(); printf("\nSuccessfully performed %d tests in %.3f milliseconds!\n\n", - rc, timeit_stop(&start)); + rc, timeit_stop(&start) * 1000); return 0; } diff --git a/src/timeit/timeit.c b/src/timeit/timeit.c index 073f8664..86c87a17 100644 --- a/src/timeit/timeit.c +++ b/src/timeit/timeit.c @@ -10,29 +10,18 @@ * */ #include +#include /* - * Usage: - * - * timeit_t start; - * timeit_start(&start); - * - * ... some code .... - * - * log_debug("Time in milliseconds: %f",timeit_stop(&start)); + * Returns time past in seconds */ -void timeit_start(timeit_t * start) -{ - gettimeofday(start, 0); -} - -float timeit_stop(timeit_t * start) +double timeit_stop(struct timespec * start) { - timeit_t end; + struct timespec end; - gettimeofday(&end, 0); + clock_gettime(CLOCK_MONOTONIC, &end); - return (end.tv_sec - start->tv_sec) * 1000.0f + - (end.tv_usec - start->tv_usec) / 1000.0f; + return (end.tv_sec - start->tv_sec) + + (end.tv_nsec - start->tv_nsec) / 1000000000.0f; }